package com.jdk.demo.io.服务单和客户端互传对象以及io流顺序问题;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

	public static void main(String[] args) throws Exception {
		Socket socket = new Socket("localhost", 8888);

		InputStream inputStream = socket.getInputStream();
		OutputStream outputStream = socket.getOutputStream();

		// 这里如果对调位置，会导致客户端阻塞在new ObjectInputStream(inputStream)
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
		ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

		for (int i = 0; i < 5; i++) {
			UserInfo userInfo = new UserInfo(i + 1, "clientUsername" + i, "clientPassword" + i);
			objectOutputStream.writeObject(userInfo);

			UserInfo userInfo2 = (UserInfo)objectInputStream.readObject();
			System.out.println("在客户端打印，第" + i + "次：" + userInfo2);
		}

		objectInputStream.close();
		objectOutputStream.close();
		outputStream.close();
		inputStream.close();
		socket.close();
	}

}
